CFBean Tags
What Are CFBean Tags?
The CFBean tags are the equivalents of the JavaServer Pages tags useBean, setProperty and getProperty, except that they operate on ColdFusion components rather than on JavaBeans.
JavaBeans are a very simple pattern for building Java objects. A JavaBean consists of private variables that are accessible through GET and SET methods on the bean.
Here's an example of a very simple JavaBean:
public class ExampleBean {
private String message = "Hello, world.";
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}JavaBeans contain properties, which are class-level variables that have both GET and SET methods with the same name as the property (just with the word "get" or "set" prepended to them). The example above contains one property called "message."
The JavaBean pattern can easily be implemented as a ColdFusion component:
<cfcomponent>
<cfset my.message = "Hello, world."/>
<cffunction name="setMessage" returnType="void">
<cfargument name="message" type="string"/>
<cfset my.message = arguments.message/>
</cffunction>
<cffunction name="getMessage" returnType="string">
<cfreturn my.message/>
</cffunction>
</cfcomponent>
Because web applications implemented in JSP often make heavy use of JavaBeans, the JSP specification defines three tags specifically for interacting with beans: useBean, setProperty, and getProperty. You can implement the bean pattern as ColdFusion components easily, so it makes sense to have comparable tags in ColdFusion.
Installing the CFBean Tags
You can install and reference all three CFBean custom tags just like any other custom tag. For instance, if the useBean tag were in the same directory as your ColdFusion page, you could reference it like this:
<cf_useBean ... >
You can also store your custom tags in a directory specified in the ColdFusion Administrator under Extensions > Custom Tag Paths. Additionally, you can use the cfimport tag to import a directory of tags and then reference them with your own prefix, like this:
<!--- This example assumes that useBean.cfm is in a directory
called cf_tags directly off the web root. ---> <cfimport taglib="/cf_tags" prefix="mm" /> <mm:useBean … >
Using the CFBean Tags
The CFBean tags are designed to make interacting with ColdFusion components (or "ColdFusion beans") fast and simple. Before getting into examples on how each tag is used, make sure you have a clear understanding of what each tag does (see Table 1).
Table 1. CFBean Tags and Their Functions
| CFBean Tag | Description |
|---|---|
| useBean | Provides a quick and convenient way to instantiate a component and store an instance in a specified scope. The useBean tag checks the specified scope for an instance of the specified component. If it finds it, it places a reference in the "caller" scope so that it is easily accessible to the rest of the page containing the useBean tag. If a component instance is not found, the useBean tag creates an instance and places that instance in both the specified scope and in the caller scope so that it is available to the rest of the page. Any code in the body of the useBean tag is only executed if a new instance of the component is created, so any initialization code that the component needs should go inside of the useBean tag. |
| setProperty | Sets properties on your component by automatically having your set functions called. (Use setProperty only after using the useBean tag. ) You can either specify a value explicitly to set on your component or, if you leave the value blank, the setProperty tag looks in the form, URL, and request scopes for a variable with the same name as the property you want to set on the component. This makes it easy to map the form, URL, and request variables to component properties. |
| getProperty | Retrieves a property from the specified component instance. (Use getProperty only after the useBean tag.) |
Each CFBean tag contains attributes that modify its behavior. Tables 2–4 describe the attributes for each tag.
Table 2. Attributes of the useBean Tag
| Attribute | Description |
|---|---|
| id | Key that stores the component in both the specified scope and the caller scope. |
| component | Full name of the component, including the package, expressed in dot notation. Do not include the .cfc extension. |
| scope | Scope in which the useBean tag looks for and places component instances. Valid options include request, session, application, and server. |
Table 3. Attributes of the setProperty Tag
| Attribute | Description |
|---|---|
| name | Attribute that corresponds to the "id" attribute of the useBean tag. It is the name of the reference to the component instance that you want to set a property on. |
| property | Name of the property you want to set. The property must have a corresponding "set" method in the component you want to set it on. To automatically map all URL, form, and request variables to properties in your component, set the value of property to "*". |
| value | Value to set the property to. If no value is specified, the setProperty tag looks in the form, URL, and request scopes for a variable by the specified name. |
Table 4. Attributes of the getProperty Tag
| Attribute | Description |
|---|---|
| name | Attribute that corresponds to the "id" attribute of the useBean tag. It is the name of the reference to the component instance that you want to get a property from. |
| property | Name of the property you want to retrieve. The property must have a corresponding "get" method that returns the value of the property. |
Example of CFBean Tags in Use
There is a very good example of how all the CFBean tags work together inside the cfbean zip file. It uses a single ColdFusion component bean called user.cfc. The user.cfc bean contains three properties: firstName, lastName, and emailAddress. The code for the component follows:
<cfcomponent>
<cffunction name="init" access="public">
<cfset my = structNew()/>
<cfset my.firstName = "">
<cfset my.lastName = "">
<cfset my.emailAddress = "">
</cffunction>
<cffunction name="setFirstName" access="public" returnType="void" output="no">
<cfargument name="firstName" required="true" type="string"/>
<cfset my.firstName = arguments.firstName/>
</cffunction>
<cffunction name="setLastName" access="public" returnType="void" output="no">
<cfargument name="lastName" required="true" type="string"/>
<cfset my.lastName = arguments.lastName/>
</cffunction>
<cffunction name="setEmailAddress" access="public" returnType="void" output="no">
<cfargument name="emailAddress" required="true" type="string"/>
<cfset my.emailAddress = arguments.emailAddress/>
</cffunction>
<cffunction name="getFirstName" access="public" returnType="string">
<cfreturn my.firstName/>
</cffunction>
<cffunction name="getLastName" access="public" returnType="string">
<cfreturn my.lastName/>
</cffunction>
<cffunction name="getEmailAddress" access="public" returnType="string">
<cfreturn my.emailAddress/>
</cffunction>
</cfcomponent>
The file called beanExample.cfm demonstrates the use of the CFBean tags. The first thing the example does is import the bean custom tags:
<cfimport taglib="/cf_tags" prefix="mm"/>
This import tag assumes that the three CFBean tags are contained in the directory cf_tags located inside your web root.
The next piece of code that is of interest is the use of useBean to create an instance of the user component and store that instance in the session scope:
<mm:useBean id="user" component="user" scope="session"> <cfinvoke component="#user#" method="init"/> </mm:usebean>
When a new user makes a request for the beanExample.cfm page, the useBean tag determines that there is no instance of the user component already in the session scope. It creates a new instance and puts it in the "caller" scope to make it available to the rest of the page; it also puts it in the session scope because the value of the scope attribute is "session." Note that since a new instance of the component is being created, the initialization code inside the useBean tag will be executed, so the component instance will have its init function called.
NOTE: In order to run the included beanExample.cfm file, you will have to copy the user.cfc file into your webroot where beanExample can find it (or modify beanExample.cfm to look for user.cfc wherever you put it).
The next piece of code to be concerned with is the use of the setProperty tag:
<mm:setProperty name="user" property="*"/>
This set property tag finds the user component in the page scope and automatically maps form, URL, and request variables to properties in the user component. The first time the page gets requested, the form, URL, and request scopes will very likely be empty, so at this point the tag does not do anything.
The next section of the page is an HTML form containing three text inputs named firstName, lastName, and emailAddress (notice how they correspond to the properties in the user component). There is no action attribute specified in the form, which means that when it gets submitted, it will submit to itself.
The last part of the page demonstrates the use of the getProperty tag:
<table border="1" cellpadding="4">
<tr>
<td align="right">First Name:</td>
<td align="left"><mm:getProperty name="user" property="firstname"/></td>
</tr>
<tr>
<td align="right">Last Name:</td>
<td align="left"><mm:getProperty name="user" property="lastName"/></td>
</tr>
<tr>
<td align="right">Email Address:</td>
<td align="left"><mm:getProperty name="user" property="emailAddress"></td>
</tr>
</table>
Because the user bean is empty at this point, the table has all blank values. Once the user fills in the firstName, lastName, and emailAddress fields and submits the form, you'll begin to see the real advantages of the CFBean tags.
The first thing that happens after the form submits to itself is that you encounter the useBean tag again. This time useBean finds an instance of the user component in the session and therefore neither instantiates a new one nor executes the code nested inside the useBean tag.
When the setProperty tag is encountered, all the variables in the form scope are automatically mapped to properties in the user component instance in the session scope. This means that the setProperty tag automatically calls each set method in the user bean, passing in the corresponding value from the form scope.
Finally, once you reach the HTML table with the getProperty tags again, the values that were submitted in the form are retrieved and displayed.
Using the CFBean tags you can create a component instance, initialize the component, process a form submission by automatically mapping its fields to a component, and display the data that was submitted in the form with a minimal amount of code. Applying the JavaBean pattern to ColdFusion components and using the CFBean tags to interact with them can save ColdFusion developers a great deal of time and code.